package com.example.demo.leetcode.classics150;

import java.util.Map;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _23找出字符串中第一个匹配项的下标
 * @description
 * @date 2023/07/15 8:53
 * <p>
 * ******************************************************
 */
public class _23找出字符串中第一个匹配项的下标 {

    public int strStr(String haystack, String needle) {
        int m = haystack.length();
        int n = needle.length();

        int left = 0;
        int right = 0;
        StringBuilder window = new StringBuilder();
        while (right < m) {
            char c = haystack.charAt(right);
//            if (c == needle.charAt(right - left)) {
            window.append(c);
//            }
            right++;
            if (right - left == n) {
                if (window.toString().equals(needle)) {
                    return left;
                } else {
                    do {
                        window.deleteCharAt(0);
                        left++;
                    } while (window.length() > 0 && window.charAt(0) != needle.charAt(0));
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        _23找出字符串中第一个匹配项的下标 s = new _23找出字符串中第一个匹配项的下标();
        System.out.println(s.strStr("mississippi", "issip"));
    }

}
